package com.hancheng.usercloud.business.controller;

import com.hancheng.commoncloud.business.entity.vo.SysUserVO;
import com.hancheng.commoncloud.framework.annotation.PassToken;
import com.hancheng.commoncloud.framework.result.PageResult;
import com.hancheng.commoncloud.framework.result.Response;
import com.hancheng.usercloud.business.entity.dto.*;
import com.hancheng.usercloud.business.entity.vo.UserLoginVO;
import com.hancheng.usercloud.business.entity.vo.UserVO;
import com.hancheng.usercloud.business.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户信息表 Controller
 *
 * @author 韩诚
 * @date 2025/06/22
 */
@Api(tags = "用户信息表")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录
     *
     * @param userLoginDTO
     * @return
     */
    @ApiOperation(value = "用户登录", notes = "账号+密码，返回token", httpMethod = "POST")
    @PassToken
    @PostMapping("/checkLogin")
    public Response<UserLoginVO> checkLogin(@RequestBody @Valid UserLoginDTO userLoginDTO) {
        UserLoginVO userLoginVO = sysUserService.checkLogin(userLoginDTO);
        return Response.ok(userLoginVO);
    }

    /**
     * 用户修改密码
     *
     * @param updateUserPasswordDTO
     * @return
     */
    @ApiOperation(value = "用户修改密码", httpMethod = "POST")
    @PostMapping("/updateUserPassword")
    public Response<Void> updateUserPassword(@RequestBody @Valid UpdateUserPasswordDTO updateUserPasswordDTO) {
        sysUserService.updateUserPassword(updateUserPasswordDTO);
        return Response.ok();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @ApiOperation(value = "获取用户信息", httpMethod = "POST")
    @PostMapping("/getUserInfo")
    public Response<UserVO> getUserInfo() {
        return Response.ok(sysUserService.getUserInfo());
    }

    /**
     * 分页查询
     *
     * @param sysUserPageListDTO
     * @return
     */
    @ApiOperation(value = "后台-用户-分页查询", httpMethod = "POST")
    @PostMapping("/getUserPageList")
    public Response<PageResult<SysUserVO>> getUserPageList(@RequestBody @Valid SysUserPageListDTO sysUserPageListDTO) {
        return Response.ok(sysUserService.getPageList(sysUserPageListDTO));
    }

    /**
     * 新增用户
     *
     * @param sysUserAddDTO
     * @return
     */
    @ApiOperation(value = "后台-用户-新增用户", httpMethod = "POST")
    @PostMapping("/addUser")
    public Response<Void> addUser(@RequestBody @Valid SysUserAddDTO sysUserAddDTO) {
        sysUserService.addUser(sysUserAddDTO);
        return Response.ok();
    }

    /**
     * 修改用户
     *
     * @param sysUserEditDTO
     * @return
     */
    @ApiOperation(value = "后台-用户-修改用户", httpMethod = "POST")
    @PostMapping("/editUser")
    public Response<Void> editUser(@RequestBody @Valid SysUserEditDTO sysUserEditDTO) {
        sysUserService.editUser(sysUserEditDTO);
        return Response.ok();
    }

    /**
     * 删除用户
     *
     * @param userIdDTO
     * @return
     */
    @ApiOperation(value = "后台-用户-删除用户", httpMethod = "POST")
    @PostMapping("/deleteUser")
    public Response<Void> deleteUser(@RequestBody @Valid UserIdDTO userIdDTO) {
        sysUserService.deleteUser(userIdDTO);
        return Response.ok();
    }
}
